/**
 * Created by ltx on 2017/10/10.
 */

const express=require('express');
const mysql=require('mysql');

var db=mysql.createPool({host: 'localhost', user: 'root', password: '123456*', database: 'myblog'});

module.exports=function () {
    var router=express.Router();

    router.get('/',function (req, res) {
        switch(req.query.act){
            case 'del':
                db.query(`DELETE FROM message_table WHERE ID=${req.query.id}`, (err, data)=>{
                    if(err){
                        console.error(err);
                        res.status(500).send('database error').end();
                    }else{
                        res.redirect('/admin/msg');
                    }
                });
                break;
            default:
                db.query('SELECT * FROM message_table', (err, msg)=>{
                    if(err){
                        console.error(err);
                        res.status(500).send('database error').end();
                    }else{
                        //console.log(msg);
                        res.render('admin/msg.ejs', {msg});
                    }
                });
                break;
        }
    });

    return router;
};